﻿$(document).ready(function () {
    GetProvince();
    $('#selectProvince').change(function () {
        GetCity($(this).val());
    });
    setDefault();
});
//获取省份
function GetProvince() {
    //初始加载状态
    var html = '';
    $.ajax({
        type: "POST",
        url: "/ajaxhandler.ashx",
        data: { "action": "GetProvince" },
        dataType: "json",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        success: function (result) {
            html = '<option value="0">请选择省份</option>';
            $.each(result, function (i, v) {
                html += '<option value="' + v.areaId + '">' + v.areaName + '</option>';
            });
            $('#selectProvince').html(html);
        }
    })
}
//获取城市
function GetCity(provinceId) {
    //初始加载状态
    var html = '<option value="0">请选择城市</option>';
    if (provinceId != 0) {
        $.ajax({
            type: "POST",
            url: "/ajaxhandler.ashx",
            data: { "action": "GetCity", "provinceId": provinceId },
            dataType: "json",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            success: function (result) {
                $.each(result, function (i, v) {
                    html += '<option value="' + v.areaId + '|' + v.areaEn + '">' + v.areaName + '</option>';
                });
                $('#selectCity').html(html);
            }
        })
    } else { $('#selectCity').html(html); }
}
//默认
function setDefault() {
    GetCity(0);
}
//选择后跳转
function CityRedirect(url) {
    var province = $('#selectProvince').val();
    var city = $('#selectCity').val();
    if (province == "0") {
        alert("请选择省份!");
        return false;
    }
    else if (city == "0") {
        alert("请选择城市!");
        return false;
    }
    else {
        //设置跳转路径
        var arr = $("#selectCity").val().split('|');
        window.location.href = url.replace("{0}", arr[1]);
    }
}
//切换城市
function ChangeCityRedirect(rtnType, urlPattern) {
    var province = $('#selectProvince').val();
    var city = $('#selectCity').val();
    if (province == "0") {
        alert("请选择省份!");
        return false;
    }
    else if (city == "0") {
        alert("请选择城市!");
        return false;
    }
    else {
        //设置跳转路径
        var arr = $("#selectCity").val().split('|');
        switch (rtnType) {
            case "PageList":
                urlPattern = urlPattern.replace('{0}', arr[1]); break;
            case "CloudList":
                urlPattern = urlPattern.replace('{0}', arr[1]); break;
            case "CityReview":
                urlPattern = urlPattern.replace('{0}', arr[1]); break;
            case "Job_InfoList":
                urlPattern = urlPattern.replace('{0}', arr[0]); break;
            case "Help_Link":
                urlPattern = urlPattern.replace('{0}', arr[0]); break;
            case "UserPost":
                urlPattern = urlPattern.replace("{0}", arr[0]); break;
            default:
                urlPattern = urlPattern.replace('{0}', arr[1]); break;
        }
        window.location.href = urlPattern;
    }
}